/*
 * File:    FileUtil.java
 * Created: 11-Nov-2007
 *
 * by Kenji Hollis <kenji@bitgatesoftware.com>
 * Released under the Creative Commons License version 2.5
 */

package com.bitgate.util.file;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * This is a generic file utility class that pulls a file from either a filesystem or a classfile.
 * 
 * @author kenji
 * @since 0.1
 */
public class FileUtil {
	
	/**
	 * Returns a stream by filename.  The filename can contain "file:" to indicate a file on the filesystem, anything
	 * else (currently) covers a resource as stream.
	 * 
	 * @param filename <code>String</code> containing the filename to load.
	 * @return <code>InputStream</code> on success.
	 * @throws IOException on any errors.
	 */
	public static InputStream getStream(String filename) throws IOException {
		String fileType = null;
		String newFilename = filename;
		int colonPosition = filename.indexOf(":");
		
		if (colonPosition > -1) {
			fileType = filename.substring(0, colonPosition).toLowerCase();
			newFilename = filename.substring(colonPosition + 1);
		} else {
			fileType = "file";
		}
		
		if (fileType.equalsIgnoreCase("file")) {
			return new FileInputStream(newFilename);
		}
		
		return FileUtil.class.getResourceAsStream(newFilename);
	}
	
}